﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Acme.BookStore2.EntityFrameworkCore
{
    public static class BookStore2DbContextModelCreatingExtensions
    {
        public static void ConfigureBookStore2(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(BookStore2Consts.DbTablePrefix + "YourEntities", BookStore2Consts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});

            builder.Entity<Vehicle>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "Vehicle", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();

                b.Property(v => v.Number).HasMaxLength(VehicleConsts.MaxNumberLength).IsRequired();
                b.Property(v => v.VIN).HasMaxLength(VehicleConsts.MaxVINLength).IsRequired();
                b.Property(v => v.LicensePlateNumber).HasMaxLength(VehicleConsts.MaxLicensePlateNumberLength);
                b.Property(v => v.Brand).HasMaxLength(VehicleConsts.MaxBrandLength);
                b.Property(v => v.TireType).HasMaxLength(VehicleConsts.MaxTireTypeLength);
                b.Property(v => v.VehicleType).HasMaxLength(VehicleConsts.MaxVehicleTypeLength);
                b.Property(v => v.Partner).HasMaxLength(VehicleConsts.MaxPartnerLength);
                b.Property(v => v.TotalFuelCost).HasColumnType("decimal(18,2)");
                b.Property(v => v.CostAmount).HasColumnType("decimal(18,2)");

                b.HasIndex(v => v.Number);
                
                b.OwnsMany(v => v.AttachFiles, a =>
                {
                    a.WithOwner().HasForeignKey("OwnerId");
                    a.Property<int>("Id");
                    a.HasKey("Id");
                });
            });

            builder.Entity<VehicleDriveRecord>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "VehicleDriveRecord", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();

                b.HasIndex(v => v.VehicleId);
            });
            
            builder.Entity<VehicleRepairRecord>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "VehicleRepairRecord", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();
                b.Property(v => v.Location).HasMaxLength(VehicleRepairRecordConsts.MaxLocationLength);
                b.Property(v => v.FollowUpPerson).HasMaxLength(VehicleRepairRecordConsts.MaxFollowUpPersonLength);
                b.Property(v => v.FailureCause).HasMaxLength(VehicleRepairRecordConsts.MaxFailureCauseLength);
                b.Property(v => v.Technician).HasMaxLength(VehicleRepairRecordConsts.MaxTechnicianLength);

                b.OwnsMany(v => v.AttachFiles, a =>
                {
                    a.WithOwner().HasForeignKey("OwnerId");
                    a.Property<int>("Id");
                    a.HasKey("Id");
                });
                
                b.HasIndex(v => v.VehicleId);
            });
            
            builder.Entity<VehicleMaintenanceRecord>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "VehicleMaintenanceRecord", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();
                b.Property(v => v.Location).HasMaxLength(VehicleMaintenanceRecordConsts.MaxLocationLength);
                b.Property(v => v.Technician).HasMaxLength(VehicleMaintenanceRecordConsts.MaxTechnicianLength);
                
                b.OwnsMany(v => v.AttachFiles, a =>
                {
                    a.WithOwner().HasForeignKey("OwnerId");
                    a.Property<int>("Id");
                    a.HasKey("Id");
                });
                
                b.HasIndex(v => v.VehicleId);
            });
            
            builder.Entity<VehicleInspectionRecord>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "VehicleInspectionRecord", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();
                
                b.Property(v => v.Cleanliness).HasMaxLength(VehicleInspectionRecordConsts.MaxCleanlinessLength);
                b.Property(v => v.TirePressure).HasMaxLength(VehicleInspectionRecordConsts.MaxTirePressureLength);
                b.Property(v => v.RemainFuel).HasMaxLength(VehicleInspectionRecordConsts.MaxRemainFuelLength);
                b.Property(v => v.EngineLight).HasMaxLength(VehicleInspectionRecordConsts.MaxEngineLightLength);
                b.Property(v => v.WindshieldFluid).HasMaxLength(VehicleInspectionRecordConsts.MaxWindshieldFluidLength);
                
                b.OwnsMany(v => v.AttachFiles, a =>
                {
                    a.WithOwner().HasForeignKey("OwnerId");
                    a.Property<int>("Id");
                    a.HasKey("Id");
                });
                
                b.HasIndex(v => v.VehicleId);
            });

            builder.Entity<WorkSchedule>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "WorkSchedule", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();
                
                b.Property(v => v.Title).HasMaxLength(WorkScheduleConsts.MaxTitleLength);
                b.Property(v => v.Description).HasMaxLength(WorkScheduleConsts.MaxDescriptionLength);
                
                b.HasMany(ws => ws.WorkScheduleItems).WithOne().HasForeignKey(wsi => wsi.WorkScheduleId).IsRequired();
            });
            
            builder.Entity<WorkScheduleItem>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "WorkScheduleItem", BookStore2Consts.DbSchema);
                
                b.ConfigureByConvention();
                
                b.Property(v => v.Stage).HasMaxLength(WorkScheduleItemConsts.MaxStageLength);
                b.Property(v => v.Route).HasMaxLength(WorkScheduleItemConsts.MaxRouteLength);
            });
            
            builder.Entity<QuestionAndAnswerPost>(b =>
            {
                b.ToTable(BookStore2Consts.DbTablePrefix + "QuestionAndAnswerPost", BookStore2Consts.DbSchema);

                b.ConfigureByConvention();
                
                b.Property(x => x.Title).IsRequired().HasMaxLength(QuestionAndAnswerPostConsts.MaxTitleLength).HasColumnName(nameof(QuestionAndAnswerPost.Title));
                b.Property(x => x.Content).IsRequired(false).HasMaxLength(QuestionAndAnswerPostConsts.MaxContentLength).HasColumnName(nameof(QuestionAndAnswerPost.Content));
                b.Property(x => x.Description).IsRequired(false).HasMaxLength(QuestionAndAnswerPostConsts.MaxDescriptionLength).HasColumnName(nameof(QuestionAndAnswerPost.Description));
            });
        }
    }
}